package com.elong.android.specialhouse.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.entity.ApartmentSearchChildDataInfo;

import java.util.ArrayList;

public class ApartmentAreaMidAdapter extends BaseAdapter{

	private Context mContext;
	private ArrayList<ApartmentSearchChildDataInfo> mData;
	private boolean isShowBottomLine = true;
	private final int COLOR_RED;
	private final int COLOR_BLACK;

	public ApartmentAreaMidAdapter(Context context, ArrayList<ApartmentSearchChildDataInfo> data) {
		mContext = context;
		mData = data;
		COLOR_RED = mContext.getResources().getColor(R.color.common_red);
		COLOR_BLACK = mContext.getResources().getColor(R.color.light_black_myelong);
	}

	/**
	 * 设置Adapter数据
	 * 
	 * @param data
	 *            数据集合
	 * @param isAddMode
	 *            增加方式 true:设置前不清除原有数据 false:先清空原有数据在添加
	 */
	public void setData(ArrayList<ApartmentSearchChildDataInfo> data, boolean isAddMode) {
		if (!isAddMode) {
			mData.clear();
		}
		mData.addAll(data);
		notifyDataSetChanged();
	}

	/**
	 * 并不刷新当前列表显示,如需立刻刷新,请手动调用notifyDataSetChanged()方法
	 * 
	 * @param isshow
	 */
	public void setBottomLineVisibility(boolean isshow) {
		isShowBottomLine = isshow;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ItemView view;
		if (convertView == null) {
			view = new ItemView(mContext);
		} else {
			view = (ItemView) convertView;
		}
		// 填充item数据
		ApartmentSearchChildDataInfo data = mData.get(position);
		view.value_tv.setText(data.getName());
		view.value_tv.setTextColor(data.isSelect() ? COLOR_RED : COLOR_BLACK);
		if (data.getChildDataInfos() == null || data.getChildDataInfos().size() <= 0) {
			// 没有下一级
			view.value_rightimage.setImageResource(R.drawable.hotelarealine);
		} else {
			view.value_rightimage.setImageResource(data.isSelect() ? R.drawable.hotelarealine_select
					: R.drawable.hotelarealine);
		}
		view.value_bottomimage.setVisibility(isShowBottomLine ? View.VISIBLE : View.GONE);
		return view;
	}

	class ItemView extends RelativeLayout {

		public TextView value_tv;// 文字描述
		public ImageView value_rightimage;// 右侧竖线描述
		public View value_bottomimage;// 底部横线描述

		public ItemView(Context context) {
			super(context);
			LayoutInflater.from(context).inflate(R.layout.apartmentareamid_adapterview, this);
			initView();
		}

		private void initView() {
			value_tv = (TextView) findViewById(R.id.value_tv);
			value_rightimage = (ImageView) findViewById(R.id.value_rightimage);
			value_bottomimage = findViewById(R.id.value_bottomimage);
		}
	}


}
